package inventoryservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type InventoryAssetCheckoutDetailPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInventoryAssetCheckoutDetailPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InventoryAssetCheckoutDetailPageLogic {
	return &InventoryAssetCheckoutDetailPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// InventoryAssetCheckoutDetailPage 分页查询资产出库明细表项
func (l *InventoryAssetCheckoutDetailPageLogic) InventoryAssetCheckoutDetailPage(in *property_service.InventoryAssetCheckoutDetailPageReq) (*property_service.InventoryAssetCheckoutDetailPageRes, error) {
	// 定义一个资产出库明细表模型对象切片用于存储查询结果
	var data []*model.InventoryAssetCheckoutDetail
	// 获取资产出库明细表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).InventoryAssetCheckoutDetail
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID))
	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		// 处理字符类型字段，进行模糊查询
		if in.Where.AssetCode != "" {
			do = do.Where(m.AssetCode.Like("%" + in.Where.AssetCode + "%")) // 资产编号
		}
		if in.Where.AssetName != "" {
			do = do.Where(m.AssetName.Like("%" + in.Where.AssetName + "%")) // 资产名称
		}
		if in.Where.Category != "" {
			do = do.Where(m.Category.Like("%" + in.Where.Category + "%")) // 分类
		}
		if in.Where.SpecModel != "" {
			do = do.Where(m.SpecModel.Like("%" + in.Where.SpecModel + "%")) // 规格型号
		}
		if in.Where.Brand != "" {
			do = do.Where(m.Brand.Like("%" + in.Where.Brand + "%")) // 品牌
		}
		if in.Where.Origin != "" {
			do = do.Where(m.Origin.Like("%" + in.Where.Origin + "%")) // 产地
		}
		if in.Where.Unit != "" {
			do = do.Where(m.Unit.Like("%" + in.Where.Unit + "%")) // 单位
		}
		if in.Where.StorageLocation != "" {
			do = do.Where(m.StorageLocation.Like("%" + in.Where.StorageLocation + "%")) // 存放地点
		}
		if in.Where.Manufacturer != "" {
			do = do.Where(m.Manufacturer.Like("%" + in.Where.Manufacturer + "%")) // 生产商
		}

		// 处理数字类型字段，进行精确查询
		if in.Where.CheckoutID > 0 {
			do = do.Where(m.CheckoutID.Eq(in.Where.CheckoutID)) // 出库单ID
		}
		if in.Where.AssetID > 0 {
			do = do.Where(m.AssetID.Eq(in.Where.AssetID)) // 资产ID
		}
		if in.Where.CategoryID > 0 {
			do = do.Where(m.CategoryID.Eq(in.Where.CategoryID)) // 分类ID
		}
		if in.Where.SpecModelID > 0 {
			do = do.Where(m.SpecModelID.Eq(in.Where.SpecModelID)) // 规格型号ID
		}
		if in.Where.BrandID > 0 {
			do = do.Where(m.BrandID.Eq(in.Where.BrandID)) // 品牌ID
		}
		if in.Where.OriginID > 0 {
			do = do.Where(m.OriginID.Eq(in.Where.OriginID)) // 产地ID
		}
		if in.Where.ManufacturerID > 0 {
			do = do.Where(m.ManufacturerID.Eq(in.Where.ManufacturerID)) // 生产商ID
		}
	}

	// 定义一个变量用于存储总记录数
	var count int64

	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.InventoryAssetCheckoutDetail{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.InventoryAssetCheckoutDetailPageRes{
		Count: count,
		List:  list.([]*property_service.InventoryAssetCheckoutDetail),
	}, nil
}
